package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.tools.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.AfterServiceInfoActivity;
import cn.xfdzx.android.apps.shop.activity.AfterServiceReturnMoneyActivity;
import cn.xfdzx.android.apps.shop.activity.MapDistributionActivity;
import cn.xfdzx.android.apps.shop.activity.remake.OrderRemakeMarketActivity;
import cn.xfdzx.android.apps.shop.activity.remake.OrderRemakeRetailActivity;
import cn.xfdzx.android.apps.shop.activity.PaymentDeskOrderActivity;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.activity.WaitingPayRetailActivity;
import cn.xfdzx.android.apps.shop.activity.XFDOrderDetailActivity;
import cn.xfdzx.android.apps.shop.activity.XFDOrderWuliuActivity;
import cn.xfdzx.android.apps.shop.activity.XFDShopDetailsActivity;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.OrderListPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.Utils;


/**
 * @author gzk
 * @date 2020/5/18 0018
 * description：
 */
public class OrderListAdapter extends RecyclerView.Adapter {

    /**
     * 待支付两种
     */
    public static final int TYPE_1 = 0;
    public static final int TYPE_2 = 1;

    /**
     * 当前类型
     */
    public int mCurrentType = TYPE_1;
    private List<OrderListBeen.DataBean> mList;


    private Context mContext;
    private final LayoutInflater mLayoutInflater;
    private OrderListPresenter mPresenter;
    private String pageType = "all";

    public OrderListAdapter(Context mContext, List<OrderListBeen.DataBean> list, OrderListPresenter mPresenter) {
        this.mContext = mContext;
        this.mList = list;
        this.mPresenter = mPresenter;

        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);

    }

    @NonNull
    @Override

    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_1) {

            return new OrderListAdapter.OrderViewHolder0(mContext, mLayoutInflater.inflate(R.layout.item_order_dai_pay1, null));
        } else if (viewType == TYPE_2) {
            return new OrderListAdapter.OrderViewHolder1(mContext, mLayoutInflater.inflate(R.layout.item_order_dai_pay2, null));
        }
        return null;

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_1) {
            OrderListAdapter.OrderViewHolder0 orderViewHolder0 = (OrderListAdapter.OrderViewHolder0) holder;
            orderViewHolder0.setData(position);

            ((OrderViewHolder0) holder).rvBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (Utils.isFastClick()) {
                        if (mList.get(position).getPaid() != 0) {
                            if ((mList.get(position).getStatus() == 1 && mList.get(position).getDelivery_method() == 1)) {
                                Intent intent = new Intent(mContext, MapDistributionActivity.class);
                                intent.putExtra("pageposition", pageType);
                                intent.putExtra("data", mList.get(position));
                                mContext.startActivity(intent);
                            } else {
                                Intent intent = new Intent(mContext, WaitingPayRetailActivity.class);
                                intent.putExtra("pageposition", pageType);
                                intent.putExtra("data", mList.get(position));
                                mContext.startActivity(intent);
                            }
                        } else {
                            Intent intent = new Intent(mContext, WaitingPayRetailActivity.class);
                            intent.putExtra("pageposition", pageType);
                            intent.putExtra("data", mList.get(position));
                            mContext.startActivity(intent);
                        }
                    }
                }
            });

            ((OrderViewHolder0) holder).viewGotoShop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ShopActivity.class);
                    intent.putExtra("shop_id", mList.get(position).getStore().getId() + "");
                    mContext.startActivity(intent);
                }
            });
        } else if (getItemViewType(position) == TYPE_2) {
            OrderListAdapter.OrderViewHolder1 orderViewHolder1 = (OrderListAdapter.OrderViewHolder1) holder;
            orderViewHolder1.setData(position);

            ((OrderViewHolder1) holder).rvBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (Utils.isFastClick()) {
                        Intent intent = new Intent(mContext, XFDOrderDetailActivity.class);
                        intent.putExtra("data", mList.get(position));
                        mContext.startActivity(intent);
                    }
                }
            });
            ((OrderViewHolder1) holder).viewGotoShop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, XFDShopDetailsActivity.class);
                    intent.putExtra("shop_id", mList.get(position).getStore().getId() + "");
                    mContext.startActivity(intent);
                }
            });
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position).getSales_mode() == 0) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    @Override
    public int getItemCount() {

        return mList.size();
    }


    public class OrderViewHolder0 extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_lookprocess)
        TextView tv_lookprocess;
        @BindView(R.id.rv_order_goods)
        RecyclerView rvOrderGoods;
        @BindView(R.id.iv_shop)
        ImageView ivShop;
        @BindView(R.id.view_goto_shop)
        View viewGotoShop;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_to_pay)
        TextView tvToPay;//去支付
        @BindView(R.id.tv_lianxi_kefu)
        TextView tvLianxiKefu;//联系客服
        @BindView(R.id.tv_another_order)
        TextView tvAnotherOrder;//再来一单
        @BindView(R.id.tv_delete_order)
        TextView tvDeleteOrder;//删除订单
        @BindView(R.id.tv_apply_refund)
        TextView tvApplyRefund;//申请退款
        @BindView(R.id.tv_look_wuliu)
        TextView tvLookWuliu;//查看物流
        @BindView(R.id.tv_confirm_the_goods)
        TextView tvConfirmTheGoods;//确认订单
        @BindView(R.id.tv_evaluation)
        TextView tvEvaluation;//  评价
        @BindView(R.id.tv_total_money)
        TextView tvTotalMoney;
        @BindView(R.id.tv_total_num)
        TextView tvTotalNum;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.tv_order_time)
        TextView tvOrderTime;
        @BindView(R.id.rv_bg)
        View rvBg;


        private Context mContext;
        private ItemAllOrderGoodsAdapter itemAllOrderGoodsAdapter;

        public OrderViewHolder0(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rvOrderGoods.setLayoutManager(linearLayoutManager);
            itemAllOrderGoodsAdapter = new ItemAllOrderGoodsAdapter(mContext);
            rvOrderGoods.setAdapter(itemAllOrderGoodsAdapter);
        }

        private void visibility(int positon) {
            if (positon == 1) {//待支付15分钟内
                tvToPay.setVisibility(View.VISIBLE);
                tvLianxiKefu.setVisibility(View.VISIBLE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setText("待支付");
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 2) {//待支付15分钟外
                tvToPay.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setText("已取消");
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_666666));
            } else if (positon == 3) {//待配送
                tvToPay.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.VISIBLE);
                tvApplyRefund.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setText("待配送");
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 4) {//待收货
                tvToPay.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.VISIBLE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setText("待收货");

                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 5) {//待评价
                tvToPay.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.VISIBLE);
                tvState.setText("待评价");
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 6) {//售后退款
                tvToPay.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.VISIBLE);
                tvApplyRefund.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 7) {//已完成，已评价显示再来一单
                tvToPay.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 17) {//支付完取消，
                tvToPay.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            }
        }

        public void setData(int position) {
            RoundedCorners roundedCorners = new RoundedCorners(20);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
            options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                    .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                    .error(R.drawable.icon_defult);
            if (null != mList.get(position).getStore()) {
                if (null != mList.get(position).getStore().getLogo()) {
                    Glide.with(mContext).load(mList.get(position).getStore().getLogo()).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivShop);
                }
                if (mList.get(position).getSales_mode() == 0) {
                    tvShopName.setText(mList.get(position).getStore().getOffline_shop_name());
                } else {
                    tvShopName.setText(mList.get(position).getStore().getName());
                }
            }
            //tvTotalMoney.setText("¥" + mList.get(position).getTotal_price());
            //价格显示包含邮费价格
            tvTotalMoney.setText("¥" + mList.get(position).getPay_price());
            tvTotalNum.setText("共" + mList.get(position).getTotal_num() + "件");
            tvOrderTime.setText(mList.get(position).getCreated_at() + "");
            itemAllOrderGoodsAdapter.setList(mList.get(position).getCartInfo());


            if (mList.get(position).getPaid() == 0) {//待支付判断

                if (mList.get(position).getMiao() > 0) {
                    visibility(1);
                    int shi = mList.get(position).getMiao() / 60;
                    int miao = mList.get(position).getMiao() - shi * 60;
                    if (miao < 10) {
                        tvToPay.setText("去支付" + shi + ":0" + miao);
                    } else {
                        tvToPay.setText("去支付" + shi + ":" + miao);
                    }
                } else {
                    visibility(2);
                }

                tvToPay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //零售去支付
                        Intent intent = new Intent(mContext, WaitingPayRetailActivity.class);
                        intent.putExtra("pageposition", pageType);
                        intent.putExtra("to_pay", true);
                        intent.putExtra("data", mList.get(position));
                        mContext.startActivity(intent);
//                        if (Utils.isFastClick()) {
//                            mPresenter.toPay("routine", mList.get(position).getPay_type(), mList.get(position).getOrder_id(), position);
//                        }
                    }
                });
                tvLianxiKefu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.isFastClick()) {
                            //callPhone("15810103429");
                            if (EmptyUtil.isNotEmpty(mList.get(position).getStore().getMobile())) {
                                callPhone(mList.get(position).getStore().getMobile());
                            } else {
                                callPhone("15810103429");
                                ToastUtils.s(mContext, "该店铺暂无电话，您将拨打平台客服电话");
                            }
                        }
                    }
                });
                tvAnotherOrder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "此功能暂未开放", Toast.LENGTH_SHORT).show();
                    }
                });
                tvDeleteOrder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "此功能暂未开放", Toast.LENGTH_SHORT).show();
                    }
                });
            } else {
                //0 待发货 1 已发货 2 已收货待评价 3 已评价 4已完成 5已关闭'6已售后
                if (mList.get(position).getStatus() == 0) {
                    visibility(3);
                    if (mList.get(position).getDelivery_method() == 2) {
                        tvState.setText("待自提");
                    }
                } else if (mList.get(position).getStatus() == 1) {
                    visibility(4);
                } else if (mList.get(position).getStatus() == 2) {
                    visibility(5);
                } else if (mList.get(position).getStatus() == 3) {//已评价
                    tvState.setText("已完成");
                    visibility(7);
                } else if (mList.get(position).getStatus() == 4) {//已完成
                    tvState.setText("已完成");
                    visibility(7);
                } else if (mList.get(position).getStatus() == 6) {//已售后
                    visibility(6);
                    tvState.setVisibility(View.VISIBLE);
                    if (mList.get(position).getRefund_status() == 1) {
                        tvState.setText("退款中");
                    } else if (mList.get(position).getRefund_status() == 2) {
                        tvState.setText("已退款");
                    } else if (mList.get(position).getRefund_status() == 3) {
                        tvState.setText("退款驳回");
                    } else if (mList.get(position).getRefund_status() == 4) {
                        tvState.setText("已取消退款");
                    } else {
                        tvState.setVisibility(View.GONE);
                    }
                } else if (mList.get(position).getStatus() == 7) {//支付的取消订单
                    visibility(17);
                    tvState.setVisibility(View.VISIBLE);
                    if (mList.get(position).getRefund_status() == 5) {
                        tvState.setText("取消中");
                    } else if (mList.get(position).getRefund_status() == 6) {
                        tvState.setText("已取消");
                    } else if (mList.get(position).getRefund_status() == 7) {
                        tvState.setText("已驳回");
                    } else {
                        tvState.setVisibility(View.GONE);
                    }
                }
                /**联系客服*/
                tvLianxiKefu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.isFastClick()) {
                            //callPhone("15810103429");
                            if (EmptyUtil.isNotEmpty(mList.get(position).getStore().getMobile())) {
                                callPhone(mList.get(position).getStore().getMobile());
                            } else {
                                callPhone("15810103429");
                                ToastUtils.s(mContext, "该店铺暂无电话，您将拨打平台客服电话");
                            }
                        }
                    }
                });
                /**申请退款*/
                tvApplyRefund.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Toast.makeText(mContext, "此功能暂未开放", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(mContext, AfterServiceReturnMoneyActivity.class);
                        intent.putExtra("type", "0");
                        intent.putExtra("orderid", mList.get(position).getOrder_id());
                        intent.putExtra("pageposition", pageType);
                        mContext.startActivity(intent);
                    }
                });
                /**查看退款进度*/
                tv_lookprocess.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, AfterServiceInfoActivity.class);
                        intent.putExtra("orderId", mList.get(position).getOrder_id());
                        intent.putExtra("type", mList.get(position).getSales_mode() + "");
                        mContext.startActivity(intent);
                    }
                });
                /**确认收获*/
                tvConfirmTheGoods.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.isFastClick()) {
                            mPresenter.confirm(mList.get(position).getOrder_id(), position);
                        }
                    }
                });
                /**再来一单*/
                tvAnotherOrder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "此功能暂未开放", Toast.LENGTH_SHORT).show();
                    }
                });
                /**评价*/
                tvEvaluation.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //调整零售评价页面
                        Intent intent = new Intent(mContext, OrderRemakeRetailActivity.class);
                        intent.putExtra("pageposition", pageType);
                        intent.putExtra("data", mList.get(position));
                        intent.putExtra("type", mList.get(position).getSales_mode() + "");
                        mContext.startActivity(intent);
                    }
                });
//

            }
        }
    }


    public class OrderViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_lookprocess)
        TextView tv_lookprocess;
        @BindView(R.id.rv_order_goods)
        RecyclerView rvOrderGoods;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.view_goto_shop)
        View viewGotoShop;
        @BindView(R.id.tv_to_pay)
        TextView tvToPay;//去支付
        @BindView(R.id.tv_lianxi_kefu)
        TextView tvLianxiKefu;//联系客服
        @BindView(R.id.tv_another_order)
        TextView tvAnotherOrder;//再来一单
        @BindView(R.id.tv_delete_order)
        TextView tvDeleteOrder;//删除订单
        @BindView(R.id.tv_apply_refund)
        TextView tvApplyRefund;//申请退款
        @BindView(R.id.tv_confirm_the_goods)
        TextView tvConfirmTheGoods;//确认订单
        @BindView(R.id.tv_look_wuliu)
        TextView tvLookWuliu;//查看物流
        @BindView(R.id.tv_evaluation)
        TextView tvEvaluation;//  评价
        @BindView(R.id.rv_bg)
        View rvBg;
        @BindView(R.id.tv_goodsnum)
        TextView tv_goodsnum;//  商品共计件数
        @BindView(R.id.tv_payprice)
        TextView tv_payprice;// 商品应付金额

        private Context mContext;
        ItemOrderXfdGoodsAdapter itemOrderXfdGoodsAdapter;

        public OrderViewHolder1(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rvOrderGoods.setLayoutManager(linearLayoutManager);
            rvOrderGoods.setItemAnimator(new DefaultItemAnimator());
            rvOrderGoods.setNestedScrollingEnabled(false);
            itemOrderXfdGoodsAdapter = new ItemOrderXfdGoodsAdapter(mContext);
            itemOrderXfdGoodsAdapter.setOnItemClickListener(new ItemClickListener() {
                @Override
                public void onItemClick(int position) {

                }
            });
            rvOrderGoods.setAdapter(itemOrderXfdGoodsAdapter);

        }

        public void setData(int position) {
            itemOrderXfdGoodsAdapter.setList(mList.get(position).getCartInfo());
            if (null != mList.get(position).getStore()) {
                if (mList.get(position).getSales_mode() == 0) {
                    tvShopName.setText(mList.get(position).getStore().getOffline_shop_name());
                } else {
                    tvShopName.setText(mList.get(position).getStore().getName());
                }
            }

            tv_goodsnum.setText("共" + mList.get(position).getTotal_num() + "件商品 合计: ");
            tv_payprice.setText("¥" + mList.get(position).getPay_price());
            if (mList.get(position).getPaid() == 0) {//未支付
                if (mList.get(position).getMiao() > 0) {
                    visibility(1);
                    int shi = mList.get(position).getMiao() / 60;
                    int miao = mList.get(position).getMiao() - shi * 60;
                    if (miao < 10) {
                        tvToPay.setText("去支付" + shi + ":0" + miao);
                    } else {
                        tvToPay.setText("去支付" + shi + ":" + miao);
                    }
                } else {
                    visibility(2);
                }

                tvLianxiKefu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.isFastClick()) {
                            //callPhone("15810103429");
                            if (EmptyUtil.isNotEmpty(mList.get(position).getStore().getMobile())) {
                                callPhone(mList.get(position).getStore().getMobile());
                            } else {
                                callPhone("15810103429");
                                ToastUtils.s(mContext, "该店铺暂无电话，您将拨打平台客服电话");
                            }

                        }
                    }
                });
                tvAnotherOrder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "此功能暂未开放", Toast.LENGTH_SHORT).show();
                    }
                });
                tvDeleteOrder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "此功能暂未开放", Toast.LENGTH_SHORT).show();
                    }
                });

                tvToPay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        mPresenter.toPay("routine", "weixin", mList.get(position).getOrder_id(), position);
                        if (Utils.isFastClick()) {
                            Intent intent = new Intent(mContext, PaymentDeskOrderActivity.class);
                            intent.putExtra("orderKey", mList.get(position).getOrder_id() + "");
                            intent.putExtra("totalMoney", mList.get(position).getPay_price() + "");
                            intent.putExtra("from", "routine");
                            mContext.startActivity(intent);
                        }
                    }
                });
            } else {//已支付
                if (mList.get(position).getStatus() == 0) {
                    visibility(3);

                } else if (mList.get(position).getStatus() == 1) {
                    visibility(4);
                    if (getItemViewType() == TYPE_2) {
                        if (mList.get(position).getDelivery_method() == 2) {
                            tvState.setText("待自提");
                        }
                    }
                } else if (mList.get(position).getStatus() == 2) {
                    visibility(5);

                } else if (mList.get(position).getStatus() == 3) {//已评价
                    tvState.setText("已完成");
                    visibility(7);
                } else if (mList.get(position).getStatus() == 4) {//已完成
                    tvState.setText("已完成");
                    visibility(7);
                } else if (mList.get(position).getStatus() == 6) {//已售后
                    visibility(6);
                    tvState.setVisibility(View.VISIBLE);
                    if (mList.get(position).getRefund_status() == 1) {
                        tvState.setText("退款中");
                    } else if (mList.get(position).getRefund_status() == 2) {
                        tvState.setText("已退款");
                    } else if (mList.get(position).getRefund_status() == 3) {
                        tvState.setText("退款驳回");
                    } else if (mList.get(position).getRefund_status() == 4) {
                        tvState.setText("已取消退款");
                    } else {
                        tvState.setVisibility(View.GONE);
                    }
                } else if (mList.get(position).getStatus() == 7) {//支付的取消订单
                    visibility(17);
                    tvState.setVisibility(View.VISIBLE);
                    if (mList.get(position).getRefund_status() == 5) {
                        tvState.setText("取消中");
                    } else if (mList.get(position).getRefund_status() == 6) {
                        tvState.setText("已取消");
                    } else if (mList.get(position).getRefund_status() == 7) {
                        tvState.setText("已驳回");
                    } else {
                        tvState.setVisibility(View.GONE);
                    }
                }

                tvLianxiKefu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.isFastClick()) {
                            //callPhone("15810103429");
                            if (EmptyUtil.isNotEmpty(mList.get(position).getStore().getMobile())) {
                                callPhone(mList.get(position).getStore().getMobile());
                            } else {
                                callPhone("15810103429");
                                ToastUtils.s(mContext, "该店铺暂无电话，您将拨打平台客服电话");
                            }
                        }
                    }
                });

                /**查看物流*/
                tvLookWuliu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.isFastClick()) {
                            Intent intent = new Intent(mContext, XFDOrderWuliuActivity.class);
                            intent.putExtra("orderId", mList.get(position).getOrder_id() + "");
                            intent.putExtra("logistics_type", mList.get(position).getLogistics_type() + "");

                            mContext.startActivity(intent);
                        }
                    }
                });
                /**确认收货*/
                tvConfirmTheGoods.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.isFastClick()) {
                            mPresenter.confirm(mList.get(position).getOrder_id(), position);
                        }
                    }
                });

                /**申请退款*/
                tvApplyRefund.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Toast.makeText(mContext, "此功能暂未开放", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(mContext, AfterServiceReturnMoneyActivity.class);
                        intent.putExtra("type", "0");
                        intent.putExtra("orderid", mList.get(position).getOrder_id());
                        intent.putExtra("pageposition", pageType);
                        mContext.startActivity(intent);
                    }
                });
                /**查看退款进度*/
                tv_lookprocess.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, AfterServiceInfoActivity.class);
                        intent.putExtra("orderId", mList.get(position).getOrder_id());
                        intent.putExtra("type", mList.get(position).getSales_mode() + "");
                        mContext.startActivity(intent);
                    }
                });
                /**确认收获*/
                tvConfirmTheGoods.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.isFastClick()) {
                            mPresenter.confirm(mList.get(position).getOrder_id(), position);
                        }
                    }
                });
                /**再来一单*/
                tvAnotherOrder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "此功能暂未开放", Toast.LENGTH_SHORT).show();
                    }
                });
                /**评价*/
                tvEvaluation.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //调整零售评价页面
                        Intent intent = new Intent(mContext, OrderRemakeMarketActivity.class);
                        intent.putExtra("pageposition", pageType);
                        intent.putExtra("data", mList.get(position));
                        intent.putExtra("type", mList.get(position).getSales_mode() + "");
                        mContext.startActivity(intent);
                    }
                });
            }

        }


        private void visibility(int positon) {
            if (positon == 1) {//待支付15分钟内
                tvToPay.setVisibility(View.VISIBLE);
                tvLianxiKefu.setVisibility(View.VISIBLE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setText("待支付");
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 2) {//待支付15分钟外
                tvToPay.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setText("已取消");
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_666666));
            } else if (positon == 3) {//待配送
                tvToPay.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.VISIBLE);
                tvApplyRefund.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setText("待发货");
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 4) {//待收货
                tvToPay.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.VISIBLE);
                tvConfirmTheGoods.setVisibility(View.VISIBLE);
                tv_lookprocess.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setText("待收货");

                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 5) {//待评价
                tvToPay.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.VISIBLE);
                tvEvaluation.setVisibility(View.VISIBLE);
                tvState.setText("已收货");
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 6) {//售后退款
                tvToPay.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.VISIBLE);
                tvApplyRefund.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 7) {//已完成，已评价显示再来一单
                tvToPay.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            } else if (positon == 17) {//支付完取消，
                tvToPay.setVisibility(View.GONE);
                tv_lookprocess.setVisibility(View.GONE);
                tvLianxiKefu.setVisibility(View.GONE);
                tvApplyRefund.setVisibility(View.GONE);
                tvAnotherOrder.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvLookWuliu.setVisibility(View.GONE);
                tvConfirmTheGoods.setVisibility(View.GONE);
                tvEvaluation.setVisibility(View.GONE);
                tvState.setTextColor(mContext.getResources().getColor(R.color.color_F93331));
            }
        }
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        mContext.startActivity(intent);
    }

    /**
     * 获取从fragment类型全部或者带评价
     */
    public void setPagePosition(String type) {
        this.pageType = type;
    }
}
